package com.monster.service.impl;

import com.monster.mapper.ReportMapper;
import com.monster.service.ReportService;
import com.monster.utils.date.DateUtils;
import com.monster.vo.BusinessReportVo;
import com.monster.vo.ReportSetmemlVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.*;


@Service
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    ReportMapper reportMapper;


    @Override
    public Map findMemberCountOfYearsAndMonth() {
        List<Map> findMemberCountOfYearsAndMonthList= reportMapper.findMemberCountOfYearsAndMonth();
        Map map = new  HashMap();
        List yearsAndMonthList = new ArrayList();
        List memberCountList = new ArrayList<>();
        for (Map findMemberCountOfYearsAndMonthMap : findMemberCountOfYearsAndMonthList) {
            yearsAndMonthList.add( findMemberCountOfYearsAndMonthMap.get("yearsandmonth"));
            memberCountList.add( findMemberCountOfYearsAndMonthMap.get("membercounts"));
        }

        map.put("yearsAndMonth",yearsAndMonthList);
        map.put("memberCount",memberCountList);
        return map;
    }

    @Override
    public List<Map> setmealProportion() {
        return  reportMapper.setmealProportion();
    }

    @Override
    public BusinessReportVo findBusinessData() {
        BusinessReportVo businessReportVo = new BusinessReportVo();
        String s = DateUtils.parseDate2String(new Date(), "yyyy-MM-dd");
        businessReportVo.setReportDate(s);
        businessReportVo.setTodayNewMember(reportMapper.todayNewMember());
        businessReportVo.setTotalMember(reportMapper.totalMember());
        businessReportVo.setThisWeekNewMember(reportMapper.thisWeekNewMember());
        businessReportVo.setThisMonthNewMember(reportMapper.thisMonthMember());
        businessReportVo.setTodayOrderNumber(reportMapper.todayOrderNumber());
        businessReportVo.setTodayVisitsNumber(reportMapper.todayVisitsNumber());
        businessReportVo.setThisWeekOrderNumber(reportMapper.thisWeekOrderNumber());
        businessReportVo.setThisWeekVisitsNumber(reportMapper.thisWeekVisitsNumber());
        businessReportVo.setThisMonthOrderNumber(reportMapper.thisMonthOrderNumber());
        businessReportVo.setThisMonthVisitsNumber(reportMapper.thisMonthVisitsNumber());
        List<ReportSetmemlVo> reportSetmemlVoList = reportMapper.findHotSetmeal();
        for (ReportSetmemlVo reportSetmemlVo : reportSetmemlVoList) {
            NumberFormat numberFormatormat = NumberFormat.getInstance();
            numberFormatormat.setMaximumFractionDigits(2);
            String result = numberFormatormat.format((float)(reportSetmemlVo.getSetmealCount()) / (float)reportMapper.findAllOrder()*100);
            reportSetmemlVo.setProportion(result);
        }
        businessReportVo.setHotSetmeal(reportSetmemlVoList);
        return businessReportVo;
    }
}
